<?php

namespace app\service;

use think\facade\Lang;

/**
 * 五行推算服务
 * 根据日期判断五行属性并返回相应结果.
 */
class FiveElementsService
{
    // 五行关系映射
    private $fiveElementsRelations = [
        'gold' => ['generate' => 'water', 'generated_by' => 'earth', 'restrain' => 'wood', 'restrained_by' => 'fire'],
        'water' => ['generate' => 'wood', 'generated_by' => 'gold', 'restrain' => 'fire', 'restrained_by' => 'earth'],
        'wood' => ['generate' => 'fire', 'generated_by' => 'water', 'restrain' => 'earth', 'restrained_by' => 'gold'],
        'fire' => ['generate' => 'earth', 'generated_by' => 'wood', 'restrain' => 'gold', 'restrained_by' => 'water'],
        'earth' => ['generate' => 'gold', 'generated_by' => 'fire', 'restrain' => 'water', 'restrained_by' => 'wood'],
    ];

    // 五行编码映射
    private $elementCodes = [];

    /**
     * 获取五行编码映射
     */
    private function getElementCodes() {
        if (empty($this->elementCodes)) {
            $this->elementCodes = [
                'gold1' => Lang::get('element_gold1'),
                'gold1a' => Lang::get('element_gold1a'),
                'gold2' => Lang::get('element_gold2'),
                'fire3' => Lang::get('element_fire3'),
                'wood4' => Lang::get('element_wood4'),
                'wood5' => Lang::get('element_wood5'),
                'wood5a' => Lang::get('element_wood5a'),
                'water6' => Lang::get('element_water6'),
                'earth7' => Lang::get('element_earth7'),
                'earth7a' => Lang::get('element_earth7a'),
                'earth8' => Lang::get('element_earth8'),
                'earth8a' => Lang::get('element_earth8a'),
            ];
        }
        return $this->elementCodes;
    }

    // 年份五行映射 - 按照立春划分年份
    private $yearRangeElements = [];

    // 特定年份五行映射
    private $yearElements = [];

    // 月份五行映射
    private $monthElements = [];

    // 日期五行映射
    private $dayElements = [];

    // 编号与五行的关系映射
    private $numberElementRelations = [];

    // 编号分组
    private $numberGroups = [];

    // 五行数字映射
    private $elementNumbers = [
        'gold1' => '1',
        'gold1a' => '1a',
        'gold2' => '2',
        'fire3' => '3',
        'wood4' => '4',
        'wood5' => '5',
        'wood5a' => '5a',
        'water6' => '6',
        'earth7' => '7',
        'earth7a' => '7a',
        'earth8' => '8',
        'earth8a' => '8a',
    ];

    /**
     * 构造函数，初始化映射数据.
     */
    public function __construct()
    {
        $this->initYearElements();
        $this->initMonthElements();
        $this->initDayElements();
        $this->initNumberGroups();
    }

    /**
     * 初始化年份五行映射.
     */
    private function initYearElements()
    {
        // 根据题目信息，初始化年份五行映射
        // 2024年立春-2044年立春节气前五行属火3
        for ($year = 2024; $year < 2044; ++$year) {
            $this->yearElements[$year] = 'fire3';
        }

        // 2044年立春-2064年立春节气前五行水6
        for ($year = 2044; $year < 2064; ++$year) {
            $this->yearElements[$year] = 'water6';
        }

        // 2064年立春-2084年立春节气前五行土7
        for ($year = 2064; $year < 2084; ++$year) {
            $this->yearElements[$year] = 'earth7';
        }

        // 2084年立春-2104年立春节气前五行木4
        for ($year = 2084; $year < 2104; ++$year) {
            $this->yearElements[$year] = 'wood4';
        }

        // 2104年立春-2124年立春节气前五行木5
        for ($year = 2104; $year < 2124; ++$year) {
            $this->yearElements[$year] = 'wood5';
        }

        // 2124年立春-2144年立春节气前五行属土(2/8)
        for ($year = 2124; $year < 2144; ++$year) {
            $this->yearElements[$year] = 'earth8';
        }

        // 特定年份的五行属性覆盖
        $this->yearElements[2017] = 'earth8';
        $this->yearElements[2018] = 'earth8a';
        $this->yearElements[2025] = 'wood5a';
        $this->yearElements[2026] = 'fire3';
        $this->yearElements[2029] = 'gold2';
        $this->yearElements[2030] = 'gold1';
        $this->yearElements[2031] = 'gold1a';
        $this->yearElements[2032] = 'water6';
        $this->yearElements[2033] = 'earth7';
        $this->yearElements[2034] = 'earth7a';
        $this->yearElements[2035] = 'wood4';
        $this->yearElements[2036] = 'wood5';
        $this->yearElements[2037] = 'wood5a';
        $this->yearElements[2038] = 'fire3';
    }

    /**
     * 初始化月份五行映射
     * 注意：月份按节气划分，如 6月芒种节气-7月小暑节气=6月.
     */
    private function initMonthElements()
    {
        // 根据题目信息，初始化月份五行映射
        $this->monthElements = [
            1 => 'earth7',
            2 => 'earth7a',
            3 => 'wood4',
            4 => 'wood5',
            5 => 'wood5a',
            6 => 'fire3',
            7 => 'earth8',
            8 => 'earth8a',
            9 => 'gold2',
            10 => 'gold1',
            11 => 'gold1a',
            12 => 'water6',
        ];
    }

    /**
     * 初始化日期五行映射.
     */
    private function initDayElements()
    {
        // 根据提供的规则初始化特定日期的五行映射
        $this->dayElements['2025-05-31'] = 'water6';
        $this->dayElements['2025-06-01'] = 'earth7';
        $this->dayElements['2025-06-02'] = 'earth7a';
        $this->dayElements['2025-06-03'] = 'wood4';
        $this->dayElements['2025-06-04'] = 'wood5';
        $this->dayElements['2025-06-05'] = 'wood5a';
        $this->dayElements['2025-06-06'] = 'fire3';
        $this->dayElements['2025-06-07'] = 'earth8';
        $this->dayElements['2025-06-08'] = 'earth8a';
        $this->dayElements['2025-06-09'] = 'gold2';
        $this->dayElements['2025-06-10'] = 'gold1';
        $this->dayElements['2025-06-11'] = 'gold1a';
        $this->dayElements['2025-06-12'] = 'water6';
        $this->dayElements['2025-06-13'] = 'earth7';
        $this->dayElements['2025-06-14'] = 'earth7a';
    }

    /**
     * 初始化编号分组.
     */
    private function initNumberGroups()
    {
        // 木、火年容易实现的编号
        $woodFireNumbers = [
            '633667', '633661', '633664', '366376', '366316', '366346', '647863', '165336',
        ];

        // 火、土年容易实现的编号
        $fireEarthNumbers = [
            '472543', '472545', '472548', '573653', '573655', '573658',
            '486744', '486746', '486747', '587854', '587856', '587857',
            '748834', '748854', '748884', '754235', '754255', '754285',
            '848844', '848864', '848874', '854245', '854265', '854275',
            '647884', '446784', '446748', '352175', '452147', '452185',
            '553257', '553275',
        ];

        // 木、水年容易实现的编号
        $woodWaterNumbers = [
            '145718', '145712', '145713', '151111', '151117', '151116',
            '245728', '245722', '245723', '251121', '251127', '251126',
            '412181', '412121', '412131', '513211', '513271', '513261',
            '426382', '426322', '426332', '527412', '527472', '527462',
            '125314', '225324', '225342', '211141',
        ];

        // 水、金年容易实现的编号
        $waterGoldNumbers = [
            '868482', '868488', '868485', '768472', '768478', '768475',
            '687828', '687888', '687858', '673627', '673687', '673657',
        ];

        // 金、土年容易实现的编号
        $goldEarthNumbers = [
            '312135', '312133', '312132', '326336', '326334', '326331',
            '131553', '131533', '131523', '231563', '231543', '231513', '353231',
        ];

        // 保存编号分组
        $this->numberGroups['wood_fire'] = $woodFireNumbers;
        $this->numberGroups['fire_earth'] = $fireEarthNumbers;
        $this->numberGroups['wood_water'] = $woodWaterNumbers;
        $this->numberGroups['water_gold'] = $waterGoldNumbers;
        $this->numberGroups['gold_earth'] = $goldEarthNumbers;

        // 设置编号与五行的关系
        foreach ($woodFireNumbers as $number) {
            $this->numberElementRelations[$number] = ['wood', 'fire'];
        }

        foreach ($fireEarthNumbers as $number) {
            $this->numberElementRelations[$number] = ['fire', 'earth'];
        }

        foreach ($woodWaterNumbers as $number) {
            $this->numberElementRelations[$number] = ['wood', 'water'];
        }

        foreach ($waterGoldNumbers as $number) {
            $this->numberElementRelations[$number] = ['water', 'gold'];
        }

        foreach ($goldEarthNumbers as $number) {
            $this->numberElementRelations[$number] = ['gold', 'earth'];
        }
    }

    /**
     * 根据日期获取五行属性.
     *
     * @param string $date 日期字符串，格式为 Y-m-d
     *
     * @return array 包含年、月、日的五行属性
     */
    public function getElementsByDate($date)
    {
        $dateObj = new \DateTime($date);
        $year = (int) $dateObj->format('Y');
        $month = (int) $dateObj->format('m');
        $day = $dateObj->format('Y-m-d');

        $result = [
            'date' => $date,
            'year' => $year,
            'month' => $month,
            'year_element' => $this->getYearElement($year),
            'month_element' => $this->getMonthElement($month),
            'day_element' => $this->getDayElement($day),
        ];

        return $result;
    }

    /**
     * 获取年份的五行属性.
     *
     * @param int $year 年份
     *
     * @return string 五行属性
     */
    private function getYearElement($year)
    {
        if (isset($this->yearElements[$year])) {
            return $this->yearElements[$year];
        }

        // 如果没有特定年份的映射，根据年份范围判断
        if ($year >= 2024 && $year < 2044) {
            return 'fire3';
        } elseif ($year >= 2044 && $year < 2064) {
            return 'water6';
        } elseif ($year >= 2064 && $year < 2084) {
            return 'earth7';
        } elseif ($year >= 2084 && $year < 2104) {
            return 'wood4';
        } elseif ($year >= 2104 && $year < 2124) {
            return 'wood5';
        } elseif ($year >= 2124 && $year < 2144) {
            return 'earth8';
        } elseif ($year >= 2144 && $year < 2164) {
            return 'gold2';
        } elseif ($year >= 2164 && $year < 2184) {
            return 'gold1';
        } elseif ($year >= 2184 && $year < 2204) {
            return 'earth8';
        } elseif ($year >= 2204 && $year < 2224) {
            return 'fire3';
        }

        // 默认返回
        return 'unknown';
    }

    /**
     * 获取月份的五行属性.
     *
     * @param int $month 月份
     *
     * @return string 五行属性
     */
    private function getMonthElement($month)
    {
        if (isset($this->monthElements[$month])) {
            return $this->monthElements[$month];
        }

        // 默认返回
        return 'unknown';
    }

    /**
     * 获取日期的五行属性.
     *
     * @param string $day 日期，格式为 Y-m-d
     *
     * @return string 五行属性
     */
    private function getDayElement($day)
    {
        if (isset($this->dayElements[$day])) {
            return $this->dayElements[$day];
        }

        // 如果没有特定日期的映射，则根据月份判断
        $dateObj = new \DateTime($day);
        $month = (int) $dateObj->format('m');

        return $this->getMonthElement($month);
    }

    /**
     * 根据生成的六位数编码查找匹配的编号.
     *
     * @param string $code 六位数编码
     *
     * @return array 匹配的编号列表
     */
    public function findMatchingNumbersByCode($code)
    {
        $matchingNumbers = [];

        // 遍历所有编号组
        foreach ($this->numberGroups as $groupKey => $numbers) {
            foreach ($numbers as $number) {
                // 编号与六位数编码的匹配逻辑
                // 可以是完全匹配、部分匹配或其他规则
                // 这里简单实现为：如果编号的前三位数字与六位数编码的前三位有两个以上相同，则视为匹配
                $matched = 0;
                for ($i = 0; $i < 3; ++$i) {
                    if (isset($number[$i]) && isset($code[$i]) && $number[$i] === $code[$i]) {
                        ++$matched;
                    }
                }

                if ($matched >= 2) {
                    // 分割group_key获取五行组合
                    $elements = explode('_', $groupKey);

                    $matchingNumbers[] = [
                        'number' => $number,
                        'elements' => $elements,
                        'group' => $groupKey,
                        'match_level' => $matched, // 匹配程度
                    ];
                }
            }
        }

        return $matchingNumbers;
    }

    /**
     * 根据日期生成六位数字符串.
     *
     * @param string $date 日期字符串，格式为 Y-m-d
     *
     * @return string 生成的六位数字符串
     */
    public function generateSixDigitCode($date)
    {
        $dateObj = new \DateTime($date);
        $year = (int) $dateObj->format('Y');
        $month = (int) $dateObj->format('m');
        $day = (int) $dateObj->format('d');
        $weekday = (int) $dateObj->format('N'); // 1-7, 1=Monday, 7=Sunday

        // 获取年、月、日对应的五行属性
        $yearElement = $this->getYearElement($year);
        $monthElement = $this->getMonthElement($month);
        $dayElement = $this->getDayElement($date);

        // 提取五行编码数字部分
        $yearNum = $this->getElementNumber($yearElement);
        $monthNum = $this->getElementNumber($monthElement);
        $dayNum = $this->getElementNumber($dayElement);

        // 生成六位数字符串
        // 使用年、月、日五行数字以及日期数字本身的组合来生成
        $digit1 = $this->convertToSingleDigit($yearNum); // 年五行数字
        $digit2 = $this->convertToSingleDigit($monthNum); // 月五行数字
        $digit3 = $this->convertToSingleDigit($dayNum); // 日五行数字
        $digit4 = $this->convertToSingleDigit($day + $month); // 日+月
        $digit5 = $this->convertToSingleDigit($weekday + $day); // 星期几+日
        $digit6 = $this->convertToSingleDigit($year % 10 + $month); // 年个位数+月

        // 组合成六位数字符串
        $sixDigitCode = $digit1 ?? ''.$digit2 ?? ''.$digit3 ?? ''.$digit4 ?? ''.$digit5 ?? ''.$digit6 ?? '';

        return $sixDigitCode;
    }

    /**
     * 从五行属性编码中提取数字.
     *
     * @param string $element 五行属性编码
     *
     * @return string 提取的数字
     */
    private function getElementNumber($element)
    {
        // 从五行编码提取数字部分（去掉字母）
        $number = preg_replace('/[^0-9]/', '', $element);

        return $number ?: '0'; // 如果没有数字则返回0
    }

    /**
     * 将数字转换为个位数（对于字符也转为对应数字）.
     *
     * @param mixed $num 输入数字或字符
     *
     * @return string 个位数字符串
     */
    private function convertToSingleDigit($num)
    {
        // 如果是字符如'1a'，先提取数字部分
        if (!is_numeric($num)) {
            $num = preg_replace('/[^0-9]/', '', $num);
        }

        // 转换为整数
        $num = (int) $num;

        // 确保是个位数（1-9）
        if ($num < 1) {
            return '1';
        } elseif ($num > 9) {
            return (string) ($num % 9 ?: 9); // 取余，如果余数为0则返回9
        }

        return (string) ($num ?? '');
    }

    /**
     * 根据日期获取匹配的编号列表.
     *
     * @param string $date 日期，格式为 Y-m-d
     *
     * @return array 匹配的编号列表
     */
    public function getNumbersByDate($date)
    {
        $elements = $this->getElementsByDate($date);
        $yearElement = $this->getBaseElement($elements['year_element']);
        $year = (int) (new \DateTime($date))->format('Y');

        // 判断是否在 2024-2044 年间（火年）
        $isFireYear2024_2044 = ($year >= 2024 && $year < 2044);

        // 判断是否在 2064-2084 年间（土年）
        $isEarthYear2064_2084 = ($year >= 2064 && $year < 2084);

        $matchedNumbers = [];

        // 根据年份五行判断匹配的编号
        if ($yearElement == 'wood' || $yearElement == 'fire') {
            // 木火年匹配木火类编号
            if (isset($this->numberGroups['wood_fire'])) {
                foreach ($this->numberGroups['wood_fire'] as $number) {
                    $matchedNumbers[] = [
                        'number' => $number,
                        'feasible' => true,
                        'level' => '较易实现',
                        'reason' => '木生火，火生火',
                    ];
                }
            }
        } elseif ($yearElement == 'fire' || $yearElement == 'earth') {
            // 火土年匹配火土类编号
            if (isset($this->numberGroups['fire_earth'])) {
                $level = '较易实现';
                $reason = '火生土，土生土';

                // 特殊情况：在 2024-2044 年间（火年）或 2064-2084 年间（土年）
                if ($isFireYear2024_2044 || $isEarthYear2064_2084) {
                    $level = '极易实现';
                    $reason = '火生火，火生土，时间生编号';
                }

                foreach ($this->numberGroups['fire_earth'] as $number) {
                    $matchedNumbers[] = [
                        'number' => $number,
                        'feasible' => true,
                        'level' => $level,
                        'reason' => $reason,
                    ];
                }
            }
        } elseif ($yearElement == 'wood' || $yearElement == 'water') {
            // 木水年匹配木水类编号
            if (isset($this->numberGroups['wood_water'])) {
                foreach ($this->numberGroups['wood_water'] as $number) {
                    $matchedNumbers[] = [
                        'number' => $number,
                        'feasible' => true,
                        'level' => '较易实现',
                        'reason' => '水生木，木生木',
                    ];
                }
            }
        } elseif ($yearElement == 'water' || $yearElement == 'gold') {
            // 水金年匹配水金类编号
            if (isset($this->numberGroups['water_gold'])) {
                foreach ($this->numberGroups['water_gold'] as $number) {
                    $matchedNumbers[] = [
                        'number' => $number,
                        'feasible' => true,
                        'level' => '较易实现',
                        'reason' => '金生水，水生水',
                    ];
                }
            }
        } elseif ($yearElement == 'gold' || $yearElement == 'earth') {
            // 金土年匹配金土类编号
            if (isset($this->numberGroups['gold_earth'])) {
                foreach ($this->numberGroups['gold_earth'] as $number) {
                    $matchedNumbers[] = [
                        'number' => $number,
                        'feasible' => true,
                        'level' => '较易实现',
                        'reason' => '土生金，金生金',
                    ];
                }
            }
        }

        return [
            'date' => $date,
            'year' => $year,
            'year_element' => $elements['year_element'],
            'base_year_element' => $yearElement,
            'matched_numbers' => $matchedNumbers,
        ];
    }

    /**
     * 根据日期判断编号是否容易实现.
     *
     * @param string $number 编号
     * @param string $date   日期，格式为 Y-m-d
     *
     * @return array 判断结果，包含是否容易实现及原因
     */
    public function checkNumberFeasibility($number, $date)
    {
        // 获取日期的五行属性
        $elements = $this->getElementsByDate($date);
        $yearElement = $this->getBaseElement($elements['year_element']);

        // 如果编号不在映射中，返回默认结果
        if (!isset($this->numberElementRelations[$number])) {
            return [
                'number' => $number,
                'date' => $date,
                'feasible' => false,
                'reason' => '编号不在已知映射中',
            ];
        }

        $numberElements = $this->numberElementRelations[$number];

        // 判断是否在 2024-2044 年间（火年）
        $year = (int) (new \DateTime($date))->format('Y');
        $isFireYear2024_2044 = ($year >= 2024 && $year < 2044);

        // 判断是否在 2064-2084 年间（土年）
        $isEarthYear2064_2084 = ($year >= 2064 && $year < 2084);

        // 特殊情况：在 2024-2044 年间（火年）且编号属于火、土类型
        if ($isFireYear2024_2044 && in_array('fire', $numberElements) && in_array('earth', $numberElements)) {
            return [
                'number' => $number,
                'date' => $date,
                'feasible' => true,
                'level' => '极易实现',
                'reason' => '火生火，火生土，时间生编号',
            ];
        }

        // 特殊情况：在 2024-2044 年间（火年）或 2064-2084 年间（土年），且编号属于火、土类型
        if (($isFireYear2024_2044 || $isEarthYear2064_2084) && in_array('fire', $numberElements) && in_array('earth', $numberElements)) {
            return [
                'number' => $number,
                'date' => $date,
                'feasible' => true,
                'level' => '极易实现',
                'reason' => '火生火，火生土，时间生编号',
            ];
        }

        // 判断年份五行与编号五行的关系
        $feasible = false;
        $reason = '';

        // 木、火年容易实现的编号
        if (($yearElement == 'wood' || $yearElement == 'fire') &&
            in_array('wood', $numberElements) && in_array('fire', $numberElements)) {
            $feasible = true;
            $reason = '木生火，火生火';
        }
        // 火、土年容易实现的编号
        elseif (($yearElement == 'fire' || $yearElement == 'earth') &&
                 in_array('fire', $numberElements) && in_array('earth', $numberElements)) {
            $feasible = true;
            $reason = '火生土，土生土';
        }
        // 木、水年容易实现的编号
        elseif (($yearElement == 'wood' || $yearElement == 'water') &&
                 in_array('wood', $numberElements) && in_array('water', $numberElements)) {
            $feasible = true;
            $reason = '水生木，木生木';
        }
        // 水、金年容易实现的编号
        elseif (($yearElement == 'water' || $yearElement == 'gold') &&
                 in_array('water', $numberElements) && in_array('gold', $numberElements)) {
            $feasible = true;
            $reason = '金生水，水生水';
        }
        // 金、土年容易实现的编号
        elseif (($yearElement == 'gold' || $yearElement == 'earth') &&
                 in_array('gold', $numberElements) && in_array('earth', $numberElements)) {
            $feasible = true;
            $reason = '土生金，金生金';
        }

        return [
            'number' => $number,
            'date' => $date,
            'year' => $year,
            'year_element' => $elements['year_element'],
            'base_year_element' => $yearElement,
            'number_elements' => $numberElements,
            'feasible' => $feasible,
            'level' => $feasible ? '较易实现' : '不易实现',
            'reason' => $feasible ? $reason : '编号与年份五行不匹配',
        ];
    }

    /**
     * 根据日期获取五行的基本属性.
     *
     * @param string $date 日期，格式为 Y-m-d
     *
     * @return array 包含年、月、日的五行基本属性
     */
    public function getBaseElementsByDate($date)
    {
        $elements = $this->getElementsByDate($date);

        $result = [
            'date' => $date,
            'year' => $elements['year'],
            'month' => $elements['month'],
            'year_base_element' => $this->getBaseElement($elements['year_element']),
            'month_base_element' => $this->getBaseElement($elements['month_element']),
            'day_base_element' => $this->getBaseElement($elements['day_element']),
        ];

        return $result;
    }

    /**
     * 获取五行的基本属性.
     *
     * @param string $element 五行属性编码，如 fire3, wood5 等
     *
     * @return string 五行基本属性（金、木、水、火、土）
     */
    private function getBaseElement($element)
    {
        if (strpos($element, 'gold') === 0) {
            return 'gold';
        } elseif (strpos($element, 'wood') === 0) {
            return 'wood';
        } elseif (strpos($element, 'water') === 0) {
            return 'water';
        } elseif (strpos($element, 'fire') === 0) {
            return 'fire';
        } elseif (strpos($element, 'earth') === 0) {
            return 'earth';
        }

        return 'unknown';
    }

    /**
     * 获取最匹配的结果文本描述.
     *
     * @param string $date 日期，格式为 Y-m-d
     *
     * @return string 匹配结果的文本描述
     */
    public function getMatchingResultText($date)
    {
        $elements = $this->getElementsByDate($date);
        $year = (int) (new \DateTime($date))->format('Y');
        $yearElement = $this->getBaseElement($elements['year_element']);

        // 判断是否在特定年份范围
        $isFireYear2024_2044 = ($year >= 2024 && $year < 2044);
        $isEarthYear2064_2084 = ($year >= 2064 && $year < 2084);
        $isWaterYear2044_2064 = ($year >= 2044 && $year < 2064);
        $isWoodYear2084_2124 = (($year >= 2084 && $year < 2104) || ($year >= 2104 && $year < 2124));
        $isGoldYear2144_2184 = (($year >= 2144 && $year < 2164) || ($year >= 2164 && $year < 2184));

        // 找出最匹配的文本描述
        if ($yearElement == 'fire' || $yearElement == 'earth') {
            // 火土年
            if ($isFireYear2024_2044 && $isEarthYear2064_2084) {
                return "在{$year}年，处于2024年-2044年之间（五行为火）或者2064年-2084年之间（五行为土），为极易实现。因为火生火，火生土，时间生编号。";
            } elseif ($isFireYear2024_2044) {
                return "在{$year}年，处于2024年-2044年之间（五行为火），为极易实现。因为火生火，时间生编号。";
            } else {
                return "在{$year}年，五行为{$yearElement}，为较易实现。因为火生土，土生土。";
            }
        } elseif ($yearElement == 'wood' || $yearElement == 'fire') {
            // 木火年
            if ($isFireYear2024_2044) {
                return "在{$year}年，处于2024年-2044年之间（五行为火），为较易实现。因为木生火，火生火。";
            } else {
                return "在{$year}年，五行为{$yearElement}，为较易实现。因为木生火，火生火。";
            }
        } elseif ($yearElement == 'wood' || $yearElement == 'water') {
            // 木水年
            if ($isWoodYear2084_2124 || $isWaterYear2044_2064) {
                return "在{$year}年，五行为{$yearElement}，为较易实现。因为水生木，木生木。";
            } else {
                return "在{$year}年，五行为{$yearElement}，为较易实现。因为水生木，木生木。";
            }
        } elseif ($yearElement == 'water' || $yearElement == 'gold') {
            // 水金年
            if ($isWaterYear2044_2064 || $isGoldYear2144_2184) {
                return "在{$year}年，五行为{$yearElement}，为较易实现。因为金生水，水生水。";
            } else {
                return "在{$year}年，五行为{$yearElement}，为较易实现。因为金生水，水生水。";
            }
        } elseif ($yearElement == 'gold' || $yearElement == 'earth') {
            // 金土年
            if ($isGoldYear2144_2184 || $isEarthYear2064_2084) {
                return "在{$year}年，五行为{$yearElement}，为较易实现。因为土生金，金生金。";
            } else {
                return "在{$year}年，五行为{$yearElement}，为较易实现。因为土生金，金生金。";
            }
        }

        // 如果没有匹配，返回空字符串
        return '';
    }

    /**
     * 主要入口方法，根据日期和编号返回分析结果.
     *
     * @param string $date   日期，格式为 Y-m-d
     * @param string $number 编号（可选）
     *
     * @return mixed 简单文本描述或详细结果（如果指定了编号）
     */
    public function analyze($date, $number = null)
    {
        // 生成六位数编码
        // $sixDigitCode = $this->generateSixDigitCode($date);

        // 如果指定了编号，返回详细的分析结果
        if ($number) {
            return $this->checkNumberFeasibility($number, $date);
        }

        // 否则返回简单的文本描述
        return $this->getMatchingResultText($date);
    }
}
